//
//  VisitorView.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/9.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit



@objc protocol VisitorViewDelegate: NSObjectProtocol {
        func loginView()
        // 注册
//        func registerView()
    // 可选
   func registerView()
}
class VisitorView: UIView {
    
    

    // 属性
    // 默认是强引用
   weak var visitorViewDelgate: VisitorViewDelegate?
    @objc func didClick() {
        visitorViewDelgate?.loginView()
    }
    @objc func didRegisterClick() {
        visitorViewDelgate?.registerView()
    }
    //MARK: 设置页面信息
    func setUIInfo(imageName: String?, title: String){
        iconView.hidden = false
        titleLable.text = title
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        } else {
            startAnimation()
        }
    }
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = 0
        anim.toValue = 2 * M_PI
        anim.duration = 20
        //当动画结束 或者视图处于非活跃状态  动画不移除图层
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }
    

    // 1.重写构造方法
    init() {
    super.init(frame: CGRectZero)
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 2.设置访客视图
    private func setupUI() {
    
        // 添加懒加载的控件
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(titleLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        // 使设置 frame 的布局失效
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        // 设置布局
        
        // VFL 手写自动布局
//        "view1.attr1 = view2.attr2 * multiplier + constant"
        // 圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 大头标的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 提示文案的约束
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        // 设置宽度约束
        addConstraint(NSLayoutConstraint(item: titleLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        // 设置登录按钮的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: titleLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // 设置注册按钮的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: titleLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil , attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置背景视图的约束
        //位移枚举 ||   []
        //对视图宽高约束的数值 [String : NSNumber]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
//
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-35)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView ,"registerBtn": registerBtn]))
        // 针对下端的白色,设置背景颜色来改变
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        // 注册点击事件
        loginBtn.addTarget(self, action: "didClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "didRegisterClick", forControlEvents: .TouchUpInside)
    }
    
 
    
    
        // 3.懒加载设置控件
        private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
        // 提示文案
    private lazy var titleLable: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
       
     private lazy var loginBtn: UIButton = UIButton(title: "登录", backImage: "common_button_white_disable", color:UIColor.lightGrayColor())
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color:UIColor.orangeColor())
    
}
